package com.fund.common.config;

import com.fund.common.enums.BaseEnum;
import com.fund.common.retention.ParamParse;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import java.lang.reflect.Field;
import java.math.BigDecimal;

public class EnumArgumentsResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(ParamParse.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter,
                                  ModelAndViewContainer mavContainer,
                                  NativeWebRequest webRequest,
                                  WebDataBinderFactory binderFactory) throws Exception {
        Class<?> paramType = parameter.getParameterType();
        Object paramObj = paramType.getDeclaredConstructor().newInstance();

        //获取该枚举所有的字段
        Field[] fields = paramType.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String paramName = field.getName();
            String[] paramValues = webRequest.getParameterValues(paramName);
            if (paramValues == null || paramValues.length == 0) {
                continue;
            }

            if(BaseEnum.class.isAssignableFrom(field.getType())){
                Enum value = convertEnumValue(field.getType(), paramValues[0]);
                field.set(paramObj, value);
            }else {
                Object value = convertCommonValue(field.getType(), paramValues[0]);
                field.set(paramObj, value);
            }

        }
        return paramObj;
    }
    private Enum convertEnumValue(Class<?> targetType, String value){
        //为空直接返回
        if(value == null || value.isEmpty()) {
            return null;
        }
        @SuppressWarnings("unchecked")
        Class<? extends BaseEnum> enumType = (Class<? extends BaseEnum>) targetType;
        try{
            Integer code = Integer.valueOf(value);
            return BaseEnum.fromCode(enumType,code);
        }catch (NumberFormatException e){
            throw new NumberFormatException("枚举必须是数字："+value);
        }
    }

    //参数转换器
    private Object convertCommonValue(Class<?> targetType, String value) {
        //为空直接返回
        if(value == null || value.isEmpty()) {
            return null;
        }

        if(targetType == String.class) {
            return value;
        }else if (targetType == Integer.class || targetType == int.class) {
            return Integer.valueOf(value);
        } else if (targetType == Long.class || targetType == long.class) {
            return Long.valueOf(value);
        } else if (targetType == Boolean.class || targetType == boolean.class) {
            return Boolean.valueOf(value);
        } else if (targetType == BigDecimal.class){
            return new BigDecimal(value);
        } else throw new IllegalArgumentException(targetType.getName()+"类型的参数转化器还没写喵，后续补充喵");

    }
}
